<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Dealer;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\Dealer as DealerModel;
use App\Admin\Actions\Grid\DealerDownloadTemplate;
use Dcat\Admin\Widgets\Modal;
use App\Admin\Forms\DealerImportForm;
use App\Admin\Renderable\VehicleSeriesTable;
use App\Models\VehicleSeries;

class DealerController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $model = Dealer::with('series');
        return Grid::make($model, function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('code');
            // $grid->column('name');
            $grid->column('name');

            $grid->column('area_code');
            $grid->column('address')->limit(20);
            // $grid->column('level')->select(DealerModel::$levelMap);
            $grid->column('level')->display(function ($level) {
                return DealerModel::$levelMap[$level];
            });
            $grid->column('service_phone');
            // $grid->column('lng');
            // $grid->column('lat');
            $grid->column('online')->switch();
            // $grid->column('online')->display(function ($show) {
            //     return $show ? '是' : '否';
            // });
            // $grid->column('series', '销售车系')->display(function ($series) {
            //     $serieses = '';
            //     foreach ($series as $v) {
            //         $serieses .= $v->series_name . ',';
            //     }
            //     return $serieses;
            // });

            $grid->column('created_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->integer()->width(3);
                $filter->like('code')->width(3);
                $filter->like('name')->width(3);
                $filter->equal('area_code')->width(3);
                $filter->equal('level')->width(3)->select('api/dealer_levels');
                $filter->equal('series.id', '销售车系')->width(3)->select('api/series');
            });
            $titles = ['id' => 'ID', 'code' => '经销商惠享账号', 'name' => '经销商名', 'area_code' => '行政区号', 'address' => '具体地址', 'jibie' => '级别', 'service_phone' => '服务电话', 'lng' => '经度', 'lat' => '纬度', 'zaiwang' => '是否在网', 'serieses' => '销售车系'];

            $grid->export()->rows(function ($rows) {
                foreach ($rows as $index => &$row) {
                    $row['zaiwang'] = $row->online ? '是' : '否';
                    $row['jibie'] = DealerModel::$levelMap[$row->level];
                    $serieses = '';
                    foreach ($row->series as $v) {
                        $serieses .= $v->series_name . ',';
                    }
                    $row['serieses'] = trim($serieses, ',');
                }
                return $rows;
            })->titles($titles)->csv();

            // 在工具栏添加操作按钮，用于上传Excel文件及下载导入模板
            $grid->tools(function (Grid\Tools $tools) {
                $tools->append(Modal::make()
                    // 弹窗标题
                    ->title('上传文件')
                    // 按钮
                    ->button('<button class="btn btn-primary"><i class="feather icon-upload"></i>&nbsp;&nbsp;导入数据</button>')
                    // 弹窗内容
                    ->body(DealerImportForm::make()));
                // 下载导入模板
                $tools->append(DealerDownloadTemplate::make());
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Dealer(), function (Show $show) {
            $show->field('id');
            $show->field('code');
            $show->field('name');
            $show->field('area_code');
            $show->field('address');
            $show->field('lat');
            $show->field('lng');
            $show->field('level')->using(DealerModel::$levelMap);
            $show->field('service_phone');
            $show->field('online');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $model = Dealer::with('series');

        return Form::make($model, function (Form $form) {
            $form->display('id');
            $form->text('code')->required();
            $form->text('name')->required();
            $form->text('area_code')->required();
            $form->text('address')->required();
            $form->text('lat')->required();
            $form->text('lng')->required();
            $form->select('level')->options('/api/dealer_levels')->required();
            $form->text('service_phone')->required();
            $form->switch('online');
            $form->multipleSelectTable('series', '销售车系')
                ->from(VehicleSeriesTable::make(['id' => $form->getKey()])) // 设置渲染类实例，并传递自定义参数
                ->options(function ($v) { // 设置编辑数据显示
                    if (!$v) {
                        return [];
                    }
                    return VehicleSeries::find($v)->pluck('series_name', 'id');
                })
                ->customFormat(function ($v) {
                    if (!$v) {
                        return [];
                    }
                    // 从数据库中查出的二维数组中转化成ID
                    return array_column($v, 'id');
                })->required();
        });
    }
}
